<?php
########################################################
# DMS Donations Management System v1.0                 #
# created by:  Isaac Sabas                             #
#			   Nikko Reyes							   #
#			   Miko Tiamwatt						   #
#                                                      #
# De La Salle Philippines                              #
# All Rights Reserved DLSP Copyright 2009              #
########################################################

define('ACCESS', true);	

session_start();	

# set error checking levels #	
	# enable these lines when site is o be deployed #	
	ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    //ini_set('log_errors', 'On');
    //ini_set('error_log', '/usr/local/apache/logs/error_log');
	
	#############################
	# include some needed files #
	#############################
		$absRoot = '../../';
		include($absRoot . 'lib/XAJAX/xajax_core/xajax.inc.php');
		include($absRoot . 'src/ClientHandle.php');

	$id = $_GET["client_id"];

	$alumni="";
	$deceased="";
	$deceasedb="disabled";
	
	$client = new ClientHandle();
	
	$ProfileArray=$client->getClientProfile($id);

	if ($ProfileArray["client_alumni"]==1)
	{
		$alumni="checked";
	}
	else if ($ProfileArray["client_alumni"]==0)
	{
		$alumni="";
	}
	
	if($ProfileArray["client_deceased"]==1)
	{
	$ClientDeceaseday=$ProfileArray["client_dateofdeath"];
	$deceased="checked";
	$deceasedb="";
	}
	else if($ProfileArray["client_deceased"]==0)
	{
	$ClientDeceaseday=date("Y-m-d");	
	$deceased="";
	$deceasedb="disabled";
	}
	$DeceaseMonth=substr($ClientDeceaseday,5,2);
	$DeceaseDay=substr($ClientDeceaseday,8,2);
	$DeceaseYear=substr($ClientDeceaseday,0,4);
	
		 $january=""; 
		 $february=""; 
		 $march=""; 
		 $april=""; 
		 $may=""; 
		 $june=""; 
		 $july=""; 
		 $august=""; 
		 $september=""; 
		 $october=""; 
		 $november=""; 
		 $december=""; 		
	
	$selectedmonth=$DeceaseMonth;	 
		 
	switch ($DeceaseMonth)
	{
		case 1: $january="selected"; break;
		case 2: $february="selected"; break;
		case 3: $march="selected"; break;
		case 4: $april="selected"; break;
		case 5: $may="selected"; break;
		case 6: $june="selected"; break;
		case 7: $july="selected"; break;
		case 8: $august="selected"; break;
		case 9: $september="selected"; break;
		case 10: $october="selected"; break;
		case 11: $november="selected"; break;
		case 12: $december="selected"; break;		
	}

# instantiate predifined classes #											
	$xajax = new xajax();

	$xajax->registerFunction("datecheck");
	
	$xajax->registerFunction("deceasecheck");
	
	$xajax->registerFunction("editClientMisc");
	
	function deceasecheck($decease) {
	$statusresponse = new xajaxResponse();	

	if($decease == true)
	{
		$statusresponse->assign('DeceaseMonth', 'disabled', '');
		$statusresponse->assign('DeceaseDay', 'disabled', '');
		$statusresponse->assign('DeceaseYear', 'disabled', '');
	}
	else if ($decease == false)
	{
		$statusresponse->assign('DeceaseMonth', 'disabled', 'true');
		$statusresponse->assign('DeceaseDay', 'disabled', 'true');
		$statusresponse->assign('DeceaseYear', 'disabled', 'true');
	}
	
	return$statusresponse;
	}
	
	function datecheck($selectedmonth, $selectedday, $selectedyear) {
		$statusresponse = new xajaxResponse();
		
		if($selectedmonth==2)
		{
		if((($selectedyear % 4 == 0) && ($selectedyear % 100 != 0)) || ($selectedyear % 400 == 0))
		{
			$statusresponse->assign('29', 'disabled', '');
			$statusresponse->assign('30', 'disabled', 'true');
			$statusresponse->assign('31', 'disabled', 'true');
			if($selectedday>29)
			{
			$statusresponse->assign($selectedday, 'selected', '');	
			$statusresponse->assign('29', 'selected', 'true');
			}
		}
		else
		{
			$statusresponse->assign('29', 'disabled', 'true');
			$statusresponse->assign('30', 'disabled', 'true');
			$statusresponse->assign('31', 'disabled', 'true');
			if($selectedday>28)
			{
			$statusresponse->assign($selectedday, 'selected', '');	
			$statusresponse->assign('28', 'selected', 'true');
			}
		}
		
		}
		else if($selectedmonth==4 || $selectedmonth==6 || $selectedmonth==9 || $selectedmonth==11)
		{
		$statusresponse->assign('29', 'disabled', '');	
		$statusresponse->assign('30', 'disabled', '');
		$statusresponse->assign('31', 'disabled', 'true');
			if($selectedday>30)
			{
			$statusresponse->assign($selectedday, 'selected', '');	
			$statusresponse->assign('30', 'selected', 'true');
			}
		}
		else if($selectedmonth==1 || $selectedmonth==3 || $selectedmonth==5 || $selectedmonth==7 || $selectedmonth==8 || $selectedmonth==10 || $selectedmonth==12)
		{
		$statusresponse->assign('29', 'disabled', '');	
		$statusresponse->assign('30', 'disabled', '');
		$statusresponse->assign('31', 'disabled', '');
		}
					
		return$statusresponse;
	}
	
	function editClientMisc($Alumni,$Decease,$DeceaseMonth,$DeceaseDay,$DeceaseYear,$id) {
		$statusresponse = new xajaxResponse();
		
		$AlumniVal=0;
		$DeceaseVal=0;
		
			
			if($Alumni == true)
				$AlumniVal=1;
				
			else if($Alumni == false)
			$AlumniVal=0;

			if($Decease == true)
				$DeceaseVal=1;
				
			else if($Decease == false)
			$DeceaseVal=0;
				
			$client = new ClientHandle();

			$ProfileArray=$client->getClientProfile($id);
			
			$alumni=$ProfileArray['client_alumni'];
			
			$decease=$ProfileArray['client_deceased'];
			
			$deceasedate=$ProfileArray['client_dateofdeath'];	
			
				$deceasemonth=substr($deceasedate,5,2);
				$deceaseday=substr($deceasedate,8,2);
				$deceaseyear=substr($deceasedate,0,4);
			
			$DeceaseDate=$DeceaseYear.'-'.$DeceaseMonth.'-'.$DeceaseDay;
				
					if ($DeceaseMonth == $deceasemonth && $DeceaseDay == $deceaseday && $DeceaseYear == $deceaseyear && $AlumniVal==$alumni && $DeceaseVal==$decease) {
						$statusresponse->assign('Status','style.color','orange');
						$statusresponse->assign('Status', 'innerHTML', 'No changes is made.<br /> Not updating..');
					}
					elseif ($id != '') {
						
						$result = $client->setProfileValue('client_alumni', $AlumniVal, $id);
						$result = $client->setProfileValue('client_deceased', $DeceaseVal, $id);	
						
						if($DeceaseVal==1)
						$result = $client->setProfileValue('client_dateofdeath', $DeceaseDate, $id);
						
						if($result == true)
						{
							$statusresponse->call("parent.window.frames['view_client_profile_".$id."'].location.reload()"); 
							$statusresponse->assign('Status','style.color','green');
							$statusresponse->assign('Status', 'innerHTML', 'Client Misc changed.<br /> Database updated.');
						}
						else
						{
							$statusresponse->assign('Status','style.color','red');
							$statusresponse->assign('Status', 'innerHTML', 'Please complete all fields.<br />Not updating..');
						}
					}
			
			
								
		return$statusresponse;
	}
	$xajax->processRequest();	

	//print_r($result);
?>

<html>
<head>
	<script type="text/javascript" src="../../lib/XAJAX/xajax_js/xajax_core.js" />
	<link rel="stylesheet" type="text/css" href="../../lib/YUI/yui/build/fonts/fonts-min.css" />
	<?php $xajax->printJavaScript(); ?>
</head>
<body onLoad="xajax_datecheck(document.getElementById('DeceaseMonth').value,document.getElementById('DeceaseDay').value,document.getElementById('DeceaseYear').value); xajax_deceasecheck(document.getElementById('Decease').checked);">											

<form id="EditClientDeceasedate" action="#" method='post'>
<table>
	
	<tr>
	<td>
	</td>
	<td>
	<input type="checkbox" id="Alumni" <?php echo $alumni?>>Alumni</input>
	</td>
	</tr>
	<tr>
	<td>
	</td> 
	<td>
	<input type="checkbox" id="Decease" name="Decease" onChange="xajax_deceasecheck(document.getElementById('Decease').checked);" <?php echo $deceased?>>Deceased</input>
	</td>
	</tr>
	<tr>
	<td>
	Month:</td>
	<td>
			<select id="DeceaseMonth" name="DeceaseMonth" onChange="xajax_datecheck(document.getElementById('DeceaseMonth').value,document.getElementById('DeceaseDay').value,document.getElementById('DeceaseYear').value);">
			<option value="1" <?php echo $january;?>>January</option>
			<option value="2" <?php echo $february;?>>February</option>
			<option value="3" <?php echo $march;?>>March</option>
			<option value="4" <?php echo $april;?>>April</option>
			<option value="5" <?php echo $may;?>>May</option>
			<option value="6" <?php echo $june;?>>June</option>
			<option value="7" <?php echo $july;?>>July</option>
			<option value="8" <?php echo $august;?>>August</option>
			<option value="9" <?php echo $september;?>>September</option>
			<option value="10" <?php echo $october;?>>October</option>
			<option value="11" <?php echo $november;?>>November</option>
			<option value="12" <?php echo $december;?>>December</option>
			</select>
	</td>
	</tr>
	<tr>
	<td>
	Day:</td>
	<td>
			<select id="DeceaseDay" name="DeceaseDay">
			<?php $i=1;
			while($i < 32)
			{?>
			<option value="<?php echo $i;?>" id="<?php echo $i;?>" <?php if($DeceaseDay==$i) echo "selected";?>><?php echo $i;?></option>
			<?php $i++; } ?>
			</select>
	</td>
	</tr>
	<tr>
	<td>
	Year:</td>
	<td>
			<select id="DeceaseYear" name="DeceaseYear" onChange="xajax_datecheck(document.getElementById('DeceaseMonth').value,document.getElementById('DeceaseDay').value,document.getElementById('DeceaseYear').value);">
			<?php $i=date("Y");
			while($i > 1919)
			{?>
			<option value="<?php echo $i;?>" id="<?php echo $i;?>" <?php if($DeceaseYear==$i) echo "selected";?>><?php echo $i;?></option>
			<?php $i--; } ?>
			</select>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	<td>
	<input type=button id="Update" value="Update" onClick="xajax_editClientMisc(document.getElementById('Alumni').checked, document.getElementById('Decease').checked, document.getElementById('DeceaseMonth').value,document.getElementById('DeceaseDay').value,document.getElementById('DeceaseYear').value, <?php echo $id;?>);"></input>
	<input type=reset id="Reset" value="Reset" onFocus="xajax_datecheck(document.getElementById('DeceaseMonth').value,document.getElementById('DeceaseDay').value,document.getElementById('DeceaseYear').value);"></input>
	</td>
	</tr>
	
	
</table>

</form>
<div id='Status'></div>

</body>

</html>